package com.lyb.desaes3desencrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import static android.R.attr.data;
import static android.R.attr.key;
import static android.util.Config.LOGD;

public class MainActivity extends AppCompatActivity {
    private EditText input, pwd;
    private TextView showInfo;
    private String mEncrypResult;
    private String mDecrypResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        input = (EditText) findViewById(R.id.input);
        pwd = (EditText) findViewById(R.id.pwd);
        showInfo = (TextView) findViewById(R.id.showInfo);
    }

    public void click(View view) {
        String data = input.getText().toString().trim();
        String pass = pwd.getText().toString().trim();
        switch (view.getId()) {
            case R.id.desEn://DES加密 密码长度8位
                //加密类型，内容，密码
                mEncrypResult = encryption("DES", data, pass);
                showInfo.setText("DES加密后的："+mEncrypResult);
                break;
            case R.id.desDe://DES解密
                mDecrypResult = decryption("DES", mEncrypResult, pass);
                showInfo.setText("DES解密后的："+ mDecrypResult);
                break;
            case R.id.aesEn://AES加密
                mEncrypResult = encryption("AES", data, pass);
                showInfo.setText("AES加密后的："+mEncrypResult);
                break;
            case R.id.aesDe://AES解密
                mDecrypResult = decryption("AES", mEncrypResult, pass);
                showInfo.setText("AES解密后的："+ mDecrypResult);
                break;
            case R.id.threeDesEn://3DES加密  密码长度24位
                mEncrypResult = encryption("DESede",data,pass);
                showInfo.setText("DESede加密后的："+mEncrypResult);
                break;
            case R.id.threeDesDe://3DES解密
                Log.d("tag", "------------------------->click: " +mEncrypResult);
                mDecrypResult = decryption("DESede", mEncrypResult, pass);
                showInfo.setText("DESede解密后的："+ mDecrypResult);
                break;
        }
    }

    //解密 三种方式  就是加密的逆运算
    private String decryption(String type, String data, String pass) {
        switch (type) {
            case "DES":
                try {
                    //1、获取解密对应的类，解密引擎 解密加密的类一样 就是init的时候模式不一样
                    Cipher cipher = Cipher.getInstance(type);
                    //2、解密引擎初始化,指定密码
                    if (pass.getBytes().length != 8) {
                        throw new Exception("密码长度必须为8位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    //3、解密 先用Base64编码
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] bytes = cipher.doFinal(encode);
                        return new String(bytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESede":
                try {
                    //1、获取解密对应的类，解密引擎 解密加密的类一样 就是init的时候模式不一样
                    Cipher cipher = Cipher.getInstance(type);
                    //2、解密引擎初始化,指定密码
                    if (pass.getBytes().length != 24) {
                        Toast.makeText(this,"密码需要24位",Toast.LENGTH_SHORT).show();
                        throw new Exception("密码长度必须为24位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    //3、解密 先用Base64编码
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] bytes = cipher.doFinal(encode);
                        return new String(bytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "AES":
                try {
                    //1、获取解密对应的类，解密引擎 解密加密的类一样 就是init的时候模式不一样
                    Cipher cipher = Cipher.getInstance(type);
                    //2、解密引擎初始化,指定密码
                    if (pass.getBytes().length != 16) {
                        Toast.makeText(this,"密码需要24位",Toast.LENGTH_SHORT).show();
                        throw new Exception("密码长度必须为24位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    //3、解密 先用Base64编码
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] bytes = cipher.doFinal(encode);
                        return new String(bytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                Toast.makeText(this, "解密类型不匹配，请使用DES、DESede、AES", Toast.LENGTH_SHORT).show();
                break;
        }
        return null;

    }

    //加密 三种方式
    private String encryption(String type, String data, String pass) {
        switch (type) {
            case "DES":
                try {
                    //1、获取加密对应的类，加密引擎
                    Cipher cipher = Cipher.getInstance(type);
                    //2、加密引擎初始化,指定密码
                    if (pass.getBytes().length != 8) {
                        throw new Exception("密码长度必须为8位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    //3、加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());
                        //加密出来的是乱码 用Base64编码
                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESede":
                try {
                    //1、获取加密对应的类，加密引擎
                    Cipher cipher = Cipher.getInstance(type);
                    //2、加密引擎初始化,指定密码
                    if (pass.getBytes().length != 24) {
                        Toast.makeText(this,"密码需要24位",Toast.LENGTH_SHORT).show();
                        throw new Exception("密码长度必须为24位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    //3、加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());
                        //加密出来的是乱码 用Base64编码
                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "AES":
                try {
                    //1、获取加密对应的类，加密引擎
                    Cipher cipher = Cipher.getInstance(type);
                    //2、加密引擎初始化,指定密码
                    if (pass.getBytes().length != 16) {
                        Toast.makeText(this,"密码需要24位",Toast.LENGTH_SHORT).show();
                        throw new Exception("密码长度必须为24位");
                    }
                    Key key = new SecretKeySpec(pass.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    //3、加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());
                        //加密出来的是乱码 用Base64编码
                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                Toast.makeText(this, "加密类型不匹配，请使用DES、DESede、AES", Toast.LENGTH_SHORT).show();
                break;
        }

        return null;
    }
}
